const faker = require("faker");
const fs = require("fs");

// 设置 Faker 为中文
faker.locale = "zh_CN";

// 资产类别列表
const assetTypes = ["办公设备", "电子设备", "机械设备", "车辆", "家具", "其他"];

// 使用部门列表
const departments = [
  "行政部",
  "财务部",
  "技术部",
  "市场部",
  "人力资源部",
  "研发部",
];

// 存放地点列表
const locations = [
  "北京总部",
  "上海分公司",
  "广州仓库",
  "深圳办事处",
  "成都研发中心",
];

// 资产状态列表
const statuses = ["在用", "闲置", "维修中", "报废"];

// 生成模拟数据
function generateAssetData(numAssets) {
  const assets = [];
  for (let i = 1; i <= numAssets; i++) {
    const asset = {
      assetId: `ASSET${String(i).padStart(6, "0")}`, // 资产编号
      assetName: faker.commerce.productName(), // 资产名称
      assetType:
        assetTypes[
          faker.datatype.number({ min: 0, max: assetTypes.length - 1 })
        ], // 资产类别
      purchaseDate: faker.date.past(5).toISOString().split("T")[0], // 购置日期（过去5年内）
      department:
        departments[
          faker.datatype.number({ min: 0, max: departments.length - 1 })
        ], // 使用部门
      location:
        locations[faker.datatype.number({ min: 0, max: locations.length - 1 })], // 存放地点
      status:
        statuses[faker.datatype.number({ min: 0, max: statuses.length - 1 })], // 资产状态
    };
    assets.push(asset);
  }
  return assets;
}

// 生成 100 条固定资产数据
const assetData = generateAssetData(100);

// 将数据保存为 CSV 文件
function saveToCSV(data, filename) {
  const header = [
    "资产编号",
    "资产名称",
    "资产类别",
    "购置日期",
    "使用部门",
    "存放地点",
    "资产状态",
  ].join(",");
  const rows = data.map((asset) =>
    [
      asset.assetId,
      asset.assetName,
      asset.assetType,
      asset.purchaseDate,
      asset.department,
      asset.location,
      asset.status,
    ].join(","),
  );
  const csvContent = [header, ...rows].join("\n");

  fs.writeFileSync(filename, csvContent, "utf8");
  console.log(`数据已保存到 ${filename}`);
}

// 保存为 CSV 文件
saveToCSV(assetData, "fixed_assets.csv");
